﻿using System;
using Newtonsoft.Json;
using TuyennvLibrary.ServerApi.Controllers;
using TuyennvLibrary.ServerApi.Models;
using TuyennvLibrary.ServerApi.Sms;
using TuyennvLibrary.ServerApi.Util;

namespace ServerApi
{
    public partial class GetPartnerSMS : BasePage
    {
        protected new void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            try
            {
                //Utilities.logError(TypeError.Sms, "Query: " + Request.QueryString);
                ProcessContent processContent = new ProcessContent();
                PartnerSMSModel partnerModel = new PartnerSMSModel();
                //sms_classproccessModel smsModel = new sms_classproccessModel();
                bool isdev = false;
                if (!string.IsNullOrEmpty(Request["dev"]))
                {
                    isdev = true;
                }
                string partner = Request["partner"];
                string username = Request["username"];
                //string key = Request["tokenkey"];
                string password = Request["password"];
                PartnerSMS partnersms = partnerModel.GetByID(int.Parse(partner));
                if (!partnersms.CheckIp(Config.Ip_Client))
                {
                    throw new Exception(string.Format("IP {0} Không được truy cập", Config.Ip_Client));
                }
                if (!partnersms.CheckAccess(username, password))
                {
                    throw new Exception(string.Format("Sai username và password"));
                }

                
                string message = Request[partnersms.param_message];
                string userid = Request[partnersms.param_userid];
                string serviceid = Request[partnersms.param_serviceid];
                string command = "";
                if (!string.IsNullOrEmpty(partnersms.param_commandcode))
                    command = Request[partnersms.param_commandcode];
                else
                {
                    command = ApiUtilities.getContentEx(message, 0);
                }
                bool isvalid = true;
                if (string.IsNullOrEmpty(message) || string.IsNullOrEmpty(userid) || string.IsNullOrEmpty(serviceid))
                    isvalid = false;
                Config.Server = Server;

                object content = JsonConvert.SerializeObject(new { status = -1, sms = "He thong dang loi, vui long thu lai sau", type = "text" });
                if (isvalid)
                {

                    MsgObject mo = new MsgObject();
                    mo.userid = userid;
                    mo.message = message;
                    mo.serviceid = serviceid;
                    mo.command_code = (!string.IsNullOrEmpty(command)) ? command : ApiUtilities.getContentEx(message, 0);
                    MsgContent msg = processContent.proccess(mo, Request, Server, isdev);
                    content = partnersms.ProccessSMS(mo, msg, Request);
                    if (content.GetType() != typeof(string))
                    {
                        content = JsonConvert.SerializeObject(content);
                    }

                }
                Response.Write(content);
            }
            catch (Exception ex)
            {
                //Utilities.logError(TypeError.Sms, ex.Message);
                Response.Write(ex.Message);
            }

        }
    }
}